<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

class DiscountCategoriesReportComponent
{
	function DiscountCategoriesReportComponent()
	{
	}

	function action(&$params, $action)
	{
		return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$languageId = (int)$params['languageId'];
		$paginate = (boolean)$params['paginate'];
		$max_rows = (int)$params['max_rows'];
		$offset = (int)$params['offset'];

		$sqlQuery = 'select dc.discount_categories_name, p.products_id, ptc.categories_id, pd.products_name, p.products_image ' .
		'from ' . TABLE_PRODUCTS . ' p, ' . TABLE_PRODUCTS_DESCRIPTION . ' pd, ' . TABLE_PRODUCTS_TO_CATEGORIES . ' ptc, ' . 
		TABLE_PRODUCTS_TO_DISCOUNT_CATEGORIES . ' ptdc,  ' . TABLE_DISCOUNT_CATEGORIES . ' dc ' .
		'where p.products_id=pd.products_id and p.products_id=ptc.products_id ' . 
		'and p.products_id=ptdc.products_id and ptdc.discount_categories_id=dc.discount_categories_id ' .
		'order by dc.discount_categories_name, pd.products_name';
		if ($paginate) {
			$sqlQuery .= ' limit ' . $offset . ', ' . $max_rows;
		}
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'name'}= $row['discount_categories_name'];
			$value{$handle}{$i}{'product_id'}= (int)$row['products_id'];
			$value{$handle}{$i}{'category_id'}= (int)$row['categories_id'];
			$value{$handle}{$i}{'product_name'}= $row['products_name'];
			$value{$handle}{$i}{'product_image'}=$row['products_image'];
			$image_size = @getimagesize(DIR_FS_CATALOG_IMAGES . 'products/' . $row['products_image']);
			$value{$handle}{$i}{'image_width'}= (int)$image_size[0];
			$value{$handle}{$i}{'image_height'}= (int)$image_size[1];
			$i++;
		}

		if ($paginate) {
			$sqlQuery = 'select count(*) as cnt from ' . TABLE_PRODUCTS_TO_DISCOUNT_CATEGORIES;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$cnt = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$cnt = (int)$row['cnt'];
			}

			$value{$handle . 'MetaData'}{'max_rows'}= (int)$max_rows;
			$value{$handle . 'MetaData'}{'offset'}= $offset;
			$value{$handle . 'MetaData'}{'page_links_max'} = (int)MAX_DISPLAY_PAGE_LINKS;
			$value{$handle . 'MetaData'}{'rows'}= $i;
			$value{$handle . 'MetaData'}{'total_rows'}= $cnt;
		}

		return $value;
	}
}
?>
